/**
 * 
 */
package com.lanswon.controller.app;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.lanswon.common.util.ImageUtil;
import com.lanswon.entity.FileObj;
import com.lanswon.entity.ImageVO;
import com.lanswon.service.AdminService;
import com.lanswon.service.ImageService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @author lanswon
 *
 */
@Controller  
@RequestMapping("/app/file")  
public class FileUploadController {  
	private static  final String  FILEURL = "/home/qzsmkweb/attachments/file/";
	private static  final String  IMGURL = "/home/qzsmkweb/attachments/img/";
	
	@RequestMapping("/manyFileUpload")
	@ResponseBody
	public Object manyFileUpload(HttpServletRequest request, ModelMap modelMap){
		Map<String, Object> viewMap = new HashMap<>();
		List<FileObj> dataList = new ArrayList<>();
		
		try {
			//创建一个通用的多部分解析器  
			CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());  
			//判断 request 是否有文件上传,即多部分请求  
			if(multipartResolver.isMultipart(request)){
				//转换成多部分request    
			    MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;  
			    //取得request中的所有文件名  
			    Iterator<String> iter = multiRequest.getFileNames();
			    //获取项目根目录
			    //String projectUrl = request.getScheme()+"://"+request.getServerName()+request.getContextPath();
			    while(iter.hasNext()){  
			        //记录上传过程起始时的时间，用来计算上传时间  
			        //int pre = (int) System.currentTimeMillis();  
			        //取得上传文件  
			        MultipartFile file = multiRequest.getFile(iter.next());
			        FileObj fo = new FileObj();
			        if(file != null){
			            //取得当前上传文件的文件名称
			            String myFileName = file.getOriginalFilename();
			            String fileType = myFileName.substring(myFileName.lastIndexOf(".")+1,myFileName.length());
			            if(("bmp/png/jpg/jpeg/gif/pdf/doc/docx/xls/xlsx/ppt/pptx/wma/mp3/wav/amr/txt").indexOf(fileType) == -1){
			            	viewMap.put("message", "文件类型不对，不能上传");
			    			viewMap.put("code", 2);
			    			return viewMap;
			            }
			            String newFileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+"."+fileType;
			            //如果名称不为“”,说明该文件存在，否则说明该文件不存在
			            if(myFileName.trim() !=""){
			            	if(ImageIO.read(file.getInputStream()) != null){
				            	try {
				            		System.out.println(fileType);
				            		String path = IMGURL + newFileName;
				            		// 上传
				            		System.out.println(path);
				            		String vmPth = "/qzsmkweb/img/"+newFileName;
									file.transferTo(new File(path));
				            		//viewMap.put("path", vmPth);
				            		fo.setFileUrl(vmPth);
								} catch (Exception e) {
									e.printStackTrace();
									viewMap.put("message", "图片上传失败");
					    			viewMap.put("code", 3);
					    			return viewMap;
								}
			            	}else{
			            		try {
				            		String path = FILEURL + newFileName;
				            		// 上传
				            		String vmPth = "/qzsmkweb/file/"+newFileName;
									file.transferTo(new File(path));
									fo.setFileUrl(vmPth);
								} catch (Exception e) {
									e.printStackTrace();
									viewMap.put("message", "文件上传失败");
					    			viewMap.put("code", 4);
					    			return viewMap;
								}
			            	}
			            }  
			        }
			        dataList.add(fo);
			    }
			}else {
				viewMap.put("code", 2);
				viewMap.put("message", "请选择上传文件");
				return viewMap;
			}
			viewMap.put("code", 0);
			viewMap.put("message", "上传成功");
			viewMap.put("data", dataList);
		} catch (Exception e) {
			e.printStackTrace();
			viewMap.put("message", "上传失败");
			viewMap.put("code", 2);
		}
		return viewMap;
	}
    
    
}  
